//
//  MainViewController.swift
//  波波微博
//
//  Created by wave on 15/7/27.
//  Copyright © 2015年 wave. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
       
        //调用方法并显示
        addChildViewControllers()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        //调用 + 号的函数并显示
        setUpMidstButton()
    }
    
//  创建tabbar中间的 + 号按钮
    private func setUpMidstButton(){
    
        //设置中间button的frame
        let w = tabBar.bounds.width / CGFloat(viewControllers!.count)
        
        let rect = CGRect(x: 0, y: 0, width: w, height: tabBar.bounds.height)
        
        midstButton.frame = CGRectOffset(rect, 2 * w, 0)
    }

//  懒加载一个按钮,放在tabbarBtn中间
    lazy private var midstButton:UIButton = {
    
        let btn = UIButton()
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        self.tabBar.addSubview(btn)
        
        //给btn添加监听方法
        btn.addTarget(self, action: "clickMidstButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
//  点击midstButton执行的方法
    func clickMidstButton(){

        let vc = UserAccount.userLogn ? ComposeViewController() : OAuthViewController()
        let nav = UINavigationController(rootViewController: vc)
        
        presentViewController(nav, animated: true, completion: nil)
    }
    
    

//  创建4个tabbar
    private func addChildViewControllers() {
        
        //首页
        addChildViewController(HomeViewController(), title: "首页", imageName: "tabbar_home")
        
        //信息
        addChildViewController(MessageViewController(), title: "发现", imageName: "tabbar_message_center")
        
        //添加一个空白的,用来给midstButton占位
        addChildViewController(UIViewController())
        
        //发现
        addChildViewController(DiscoverViewController(), title: "信息", imageName: "tabbar_discover")
        
        //我
        addChildViewController(ProfileViewController(), title: "我", imageName: "tabbar_profile")
    }
    

    
//  写一个tabbar方法,创建4个tabbar
    private func addChildViewController(vc:UIViewController,title:String,imageName:String) {
        
//      设置tabbar的颜色
//        tabBar.tintColor = UIColor.orangeColor()
        
//      设置tabbar的一些属性
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: imageName)
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
    }


    

}
